package com.example;

import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.Select;
import ru.esteru.selenium.factory.WebDriverFactory;
import org.openqa.selenium.support.PageFactory;

import com.example.pages.InternalPage;
import com.example.pages.LoginPage;
import com.example.pages.MovieListPage;
import com.example.pages.MoviePage;
import com.example.pages.MovieEditPage;


public class t04_editFilmInfo {
  private WebDriver driver;
  private String baseUrl;
  private boolean acceptNextAlert = true;
  private StringBuffer verificationErrors = new StringBuffer();
  private LoginPage LoginPage;
  private InternalPage InternalPage1;
  private MovieListPage ListFilmPage;
  private MoviePage FilmPage;
  private MovieEditPage EditFilmInfo;
  
  @Before
  public void setUp() throws Exception {
    driver = WebDriverFactory.getDriver(DesiredCapabilities.firefox());  
    //driver = new FirefoxDriver();
    baseUrl = "http://localhost/";
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
  }

  @Test
  public void testUntitled() throws Exception {
    goToMainForm();
    loginAs("admin", "admin");
    // This not understand
    driver.findElement(By.cssSelector("img[alt=\"DVD 01\"]")).click();
    driver.findElement(By.cssSelector("img[alt=\"Edit\"]")).click();
    //
    //driver.findElement(By.name("aka")).clear();
    //driver.findElement(By.name("aka")).sendKeys("hhh");
    EditFilmInfo.fldEditAKA.clear();
    EditFilmInfo.fldEditAKA.sendKeys("hhh");
    //driver.findElement(By.cssSelector("img[alt=\"Save\"]")).click();
    EditFilmInfo.btnEditFilmInfo.click();
    
    InternalPage1.menuHomeLink.click();
    logout();
  }

private void logout() {
  InternalPage1.menuLogoutLink.click();
  assertTrue(closeAlertAndGetItsText().matches("^Are you sure you want to log out[\\s\\S]$"));
}

private void loginAs(String username, String password) {
	LoginPage.userNameField.clear();
	LoginPage.userNameField.sendKeys(username);
	LoginPage.passwordField.clear();
	LoginPage.passwordField.sendKeys(password);
	LoginPage.loginButton.click();
	
}

private void goToMainForm() {
	driver.get(baseUrl + "/php4dvd/");
	LoginPage = PageFactory.initElements(driver, LoginPage.class);
	InternalPage1 = PageFactory.initElements(driver, InternalPage.class);
	
}

@After
  public void tearDown() throws Exception {
    driver.quit();
    String verificationErrorString = verificationErrors.toString();
    if (!"".equals(verificationErrorString)) {
      fail(verificationErrorString);
    }
  }

  private boolean isElementPresent(By by) {
    try {
      driver.findElement(by);
      return true;
    } catch (NoSuchElementException e) {
      return false;
    }
  }

  private String closeAlertAndGetItsText() {
    try {
      Alert alert = driver.switchTo().alert();
      if (acceptNextAlert) {
        alert.accept();
      } else {
        alert.dismiss();
      }
      return alert.getText();
    } finally {
      acceptNextAlert = true;
    }
  }
}
